package main.java.thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SyncDemo5 {
    public static void main(String[] args) {
//        List<Integer> list = new ArrayList<>();
        //List<Integer> list = new LinkedList<>();
        List<Integer> list = Collections.synchronizedList(new ArrayList<>());

        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    list.add(i);
                }
            }
        };
        Thread t2 = new Thread() {
            public void run() {
                for (int i = 1000; i < 2000; i++) {
                    list.add(i);
                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
